package com.zhkj.shixi.tvfilemanager.view;

import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.open.androidtvwidget.bridge.EffectNoDrawBridge;
import com.open.androidtvwidget.view.GridViewTV;
import com.open.androidtvwidget.view.MainUpView;
import com.zhkj.shixi.tvfilemanager.FileListActivity;
import com.zhkj.shixi.tvfilemanager.FileManager;
import com.zhkj.shixi.tvfilemanager.FileMenuCtrl;
import com.zhkj.shixi.tvfilemanager.FileSearch;
import com.zhkj.shixi.tvfilemanager.Interface.IAdapterNotify;
import com.zhkj.shixi.tvfilemanager.R;
import com.zhkj.shixi.tvfilemanager.ViewEntity;
import com.zhkj.shixi.tvfilemanager.adapter.GridViewAdapter;
import com.zhkj.shixi.tvfilemanager.base.BasePager;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.zhkj.shixi.tvfilemanager.StaticClasses.deviceList;
import static com.zhkj.shixi.tvfilemanager.StaticClasses.fileManager;

/**
 * Created by nieshuang on 19-8-13.
 */

public class GrideViewPager extends BasePager implements IAdapterNotify{

    private static GridViewAdapter gridViewAdapter;
    private static ArrayList<ViewEntity> mArrayList = new ArrayList<>();
    public static HashMap<Integer,Integer> bMap = new HashMap<>();
    private static GridViewTV mGridView;
    public static List<File> fileList;
    private FileListActivity mActivity;
    private final String TAG = "GrideViewPager";

    public FileSearch getFileSearch() {
        return fileSearch;
    }

    public static FileSearch fileSearch;
    private static MainUpView mMainUpView;

    public boolean ismIsOver() {
        return mIsOver;
    }

    public static boolean mIsOver =false;
    private static View mOldView;
    private static int mOldPosition = 0;
    private int mSavePos = 0;
    private static EffectNoDrawBridge mBridget;


    public GrideViewPager(Context context, int resource) {
        super(context, resource);
        mActivity = (FileListActivity) context;
    }
    //页面载入必须实现的方法，可以显示不同的效果
    @Override
    public View initView(int resource) {
        View view = View.inflate(context,resource,null);
        mGridView = (GridViewTV) view.findViewById(R.id.gird_view);
        mMainUpView = (MainUpView) view.findViewById(R.id.mainUpView);
        fileSearch = new FileSearch(mActivity);
        onFocusMove();
        return view;
    }

    //页面绑定数据的处理
    public void callBack(){
        mIsOver=true;
        Log.i(TAG, "run: "+fileSearch.getAllMediaFiles());
    }
    @Override
    public void initData(){
        super.initData();
        if(deviceList .isEmpty()) {
            Log.e("Device","没有设备");
            return;
        }
        if(fileList==null)fileList = deviceList;
        gridViewAdapter= new GridViewAdapter(context,mArrayList);
        gridViewAdapter.setIAdapterNotifyCallback(this);
        flushViewData(fileList);
        mGridView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!mGridView.hasFocus()){
                    mBridget.setVisibleWidget(true);

                }else{
                    mGridView.setSelection(0);
//                    mGridView.requestFocus();
                    mBridget.setVisibleWidget(false);
                }
            }
        });
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if(mGridView.getCount() == 0){
                    mBridget.setVisibleWidget(true);
                }else{
                    mBridget.setVisibleWidget(false);
                }

                if(view == null){
                    Log.e(TAG,"view is click null");
                }else{
                    Log.e(TAG,"view is click not null position =" + position + " view postion =" + mGridView.getPositionForView(view));
                }

                Log.e(TAG,"===============6");
                ImageView girdSelected = view.findViewById(R.id.girdSelected);
                String tPath="";
                Thread thread=null;
                if(fileManager.getCurrentURL()==null)
                {
                    fileManager.setCurrentURL(fileList.get(position).getAbsolutePath());
                    if(!tPath.equals(fileManager.getCurrentURL())){
                        if(mIsOver){
                            mIsOver=false;
                            fileSearch.getAllMediaFiles().clear();
                        }

                        thread =new Thread(new Runnable() {
                            @Override
                            public void run() {
                                String rootUrl = fileManager.getCurrentURL();
                                Log.w("URLS",fileManager.getCurrentURL());
                                fileSearch.fileFilterF(rootUrl);
                                callBack();
                            }
                        });
                        thread.start();
                    }
                    tPath=fileManager.getCurrentURL();
                    fileManager.setmUSB(position);
                    TextView textView = mActivity.findViewById(R.id.returnText);
                    textView.setText(textView.getText() + " " + fileList.get(position).getName());
                }
                //fileList = fileManager.getTypeFiles(fileManager.getCurrentURL(),fileManager.getType());
                FileMenuCtrl fileCtrl = mActivity.getFileMenuCtrl();
                File file = fileList.get(position);
                if (mActivity.isSlctStat()) {
                    if (girdSelected.getVisibility() == View.INVISIBLE){
                        girdSelected.setVisibility(View.VISIBLE);
                        fileCtrl.addFile(file);
                        Log.e("onItemClick: ", "1ci"+String.valueOf(position));
                        Log.w("fileadd",file.getName());
                    }else {
                        girdSelected.setVisibility(View.INVISIBLE);
                        fileCtrl.removeFile(file);
                        Log.w("fileremove",file.getName());
                        Log.e("onItemClick: ", "2ci"+String.valueOf(position));
                    }

                    bMap.put(position,girdSelected.getVisibility());
                } else {
                    fileCtrl.clickFile(file);
                    if(!FileListActivity.searchMode) {
                        fileList = fileManager.getTypeFiles(fileManager.getCurrentURL(), fileManager.getType());
                        bMap.clear();
                        flushViewData(fileList);
                        gridViewAdapter.notifyDataSetChanged();
                    }
                }
                gridViewAdapter.notifyDataSetChanged();
            }
        });
    }

    public static void flushViewData(List<File> fileList){
        mArrayList.clear();
        int i = 0 ;
        for (File file:fileList) {
            ViewEntity viewEntity = new ViewEntity();
            viewEntity.setmFile(file);
            if (file.isDirectory()){
                if(file.getParent().equals(deviceList.get(i).getParent())) {
                    viewEntity.setmImageId(R.drawable.audio_icn_normal2);
                    i++;
                }
                else {
                    viewEntity.setmImageId(R.drawable.audio_usb_icn_folder_normal);
                    Log.w("folder",file.getName());
                }
            }else {
                if (fileManager.getType() == FileManager.MUSIC_TYPE) {
                    viewEntity.setmImageId(R.drawable.audio_usb_icn_audio_normal);
                } else if (fileManager.getType() == FileManager.VIDEO_TYPE) {
                    viewEntity.setmImageId(R.drawable.video_usb_icn_video_normal);
                } else if (fileManager.getType() == FileManager.IMAGE_TYPE) {
                    viewEntity.setmImageId(R.drawable.video_usb_icn_img_pressed);
                    Log.w("draw",file.getName());
                }
            }
            viewEntity.setmFileName(file.getName());
            mArrayList.add(viewEntity);
        }
        gridViewAdapter.setmArrayList(mArrayList);
        mGridView.setAdapter(gridViewAdapter);

        mGridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
                Log.e("cnk","==================position=" + position + "count =" + mGridView.getCount());

                if (view != null) {
                    Log.e("cnk","view position =" + mGridView.getPositionForView(view));
                    mMainUpView.setFocusView(view,mOldView, 1.0f);
                    if (mGridView.getCount() != 0) {
                        mBridget.setVisibleWidget(false);
                    }else {
                        mBridget.setVisibleWidget(true);
                    }
                    mOldView = view;
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

    }

    private void onFocusMove(){
        mMainUpView.setEffectBridge(new EffectNoDrawBridge());
        mBridget = (EffectNoDrawBridge) mMainUpView.getEffectBridge();
        mBridget.setTranDurAnimTime(150);
        // 设置移动边框的图片.
        mMainUpView.setUpRectResource(R.drawable.white_light_10);
        // 移动方框缩小的距离.
        mMainUpView.setDrawUpRectPadding(new Rect(0, -20, 0, -25));
    }

    @Override
    public void notifyDataCompleteChanged(View view) {
        Log.e("cnk","notifyDataCompleteChanged");
        if(view != null){
            mBridget.setVisibleWidget(false);
            mMainUpView.setFocusView(view,1,1.0f);
        }
    }
}